package com.thinker.plantForm.controller;

import com.thinker.plantForm.mapper.CustomerMapper;
import com.thinker.plantForm.model.Customer;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by hezi on 2017/9/7.
 */
@Controller
public class CustomerController {
    public CustomerController(CustomerMapper customerMapper) {
        this.customerMapper = customerMapper;
    }

    @RequestMapping("/thing")
    public Customer thing() {
        return new Customer();
    }

    @RequestMapping("/a")
    public String b(Map<String, Object> model) {
        model.put("msg", "张三");
        return "regiester";
    }

    @RequestMapping("/b")
    public ModelAndView b(HttpServletRequest request) {
        ModelAndView view = new ModelAndView();
        view.setViewName("regiester");
        request.setAttribute("msg", "Davie");
        return view;
    }

    /**
     * 测试hello
     *
     * @return
     */
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String hello(Model model) {
        model.addAttribute("name", "Dear");
        return "hello";
    }
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index() {
        return "index";
    }

    @RequestMapping("/list")
    public String list(HttpServletRequest request) {
        request.setAttribute("name", "dear");
        return "/hello";
    }
    @RequestMapping("/table")
    public String table(HttpServletRequest request) {
        return "/tables";
    }

    private CustomerMapper customerMapper;

    @RequestMapping("/select")
    @ResponseBody
    public Customer select(HttpServletRequest request) {
        Customer customer = customerMapper.selectById(1);
        System.out.println(customer);
//        request.setAttribute("name", customer.toString());
        return customer;
    }

    @RequestMapping(value = "/select/{id}", method = RequestMethod.GET)
    @ResponseBody
    public List<Customer> select2(HttpServletRequest request, @PathVariable int id) {
        List<Customer> list = new ArrayList<>();
        Customer customer = customerMapper.selectById(id);
        System.out.println(customer);
//        request.setAttribute("name", customer.toString());
        list.add(customer);
        return list;
    }

}

